﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectionLayer;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace BusinessLayer
{
    public static class FuncionarioActions
    {
        public static bool ResponsavelFuncionario(String dep, String ofic, String numFunc)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "AddRespOficinaDepartamento";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@dep", dep);
                cmd.Parameters.AddWithValue("@ofic", ofic);
                cmd.Parameters.AddWithValue("@numFunc", numFunc);
                SqlParameter retorno = cmd.Parameters.Add("@res", SqlDbType.Int);
                retorno.Direction = ParameterDirection.ReturnValue;
                cmd.ExecuteNonQuery();
                if ((int)retorno.Value == -1)
                {
                    MessageBox.Show("Não existe departamento Administração Geral OU o funcionario não pertence ao mesmo");
                    return false;
                }
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }
        public static bool HabilitarFuncionario(String codFunc, String codOfic, String codDep, String codActo)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "HabilitarFuncionarioActo";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@codFunc", codFunc);
                cmd.Parameters.AddWithValue("@codOfic", codOfic);
                cmd.Parameters.AddWithValue("@codDep", codDep);
                cmd.Parameters.AddWithValue("@codActo", codActo);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }
        public static bool CriarFuncionario(String nome, String NIF, String telefone, String email, String estado, String idProf)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "insert into Funcionario (nifFunc, nomeFunc,telefoneFunc, emailFunc, estadoFunc,profissao)"+ 
                    "values(@NIF,@nome,@telefone,@email,@estado,@idProf)";
                cmd.Parameters.AddWithValue("@NIF", NIF);
                cmd.Parameters.AddWithValue("@nome", nome);
                cmd.Parameters.AddWithValue("@telefone", telefone);
                cmd.Parameters.AddWithValue("@email", email);
                cmd.Parameters.AddWithValue("@estado", estado);
                cmd.Parameters.AddWithValue("@idProf", idProf);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }

        public static bool AlterarFuncionario(String codFunc, String idDep, String idOfic)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "update Funcionario set departamento = @departamento, oficina = @oficina where codFunc = @codFunc";
                cmd.Parameters.AddWithValue("@departamento", idDep);
                cmd.Parameters.AddWithValue("@oficina", idOfic);
                cmd.Parameters.AddWithValue("@codFunc", codFunc);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }
    }
}
